#include "tinyxml2.h"
#include <iostream>
#include <string>
#include <regex>

using std::cout;
using std::endl;
using std::cerr;
using std::string;
using std::regex;
using namespace tinyxml2;

void test()
{
    XMLDocument doc;
    doc.LoadFile("coolshell.xml");

    if(doc.ErrorID())
    {
        cerr << "open file error!" << endl;
        return;
    }

    XMLElement *item = doc.FirstChildElement("rss")
        ->FirstChildElement("channel")
        ->FirstChildElement("item");
    /* if(item) */
    while(item)
    {
        string title = 
            item->FirstChildElement("title")->GetText();
        string link = 
            item->FirstChildElement("link")->GetText();
        string description = 
            item->FirstChildElement("description")->GetText();
        string content = 
            item->FirstChildElement("content:encoded")->GetText();

        regex rg("<[^>]+>");
        description = regex_replace(description, rg,"");
        content = regex_replace(content, rg,"");
    
        cout << "title = " << title << endl;
        cout << "link = " << link << endl;
        cout << "description = " << description << endl;
        cout << "content = " << content << endl;

        //找item的兄弟节点（找到下一个item）
        item = item->NextSiblingElement("item");
    }
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

